#include "Reactor.hpp"
#include "Sock.hpp"
#include "Util.hpp"
#include "Accepter.hpp"

void Usage(char *proc)
{
    std::cout << "Usage:" << proc << " port " << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
    }

    int listen_sock = Sock::Socket();
    uint16_t port = atoi(argv[1]);
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);
    // 设置为非阻塞
    SetNonBlock(listen_sock);
    // 创建反应堆
    Reactor *R = new Reactor();
    // 初始化反应堆
    R->Initializer();
    // 将监听sock放入反应堆
    Event *listen_ev = new Event(listen_sock);
    listen_ev->_sock = listen_sock;
    listen_ev->R = R;
    listen_ev->CallbackRegister(Accepter, nullptr, nullptr);
    R->InsertEvent(listen_ev, EPOLLIN | EPOLLET);
    int timeout = 1000;
    while (true)
    {
        R->Dispatcher(timeout);
    }

    return 0;
}